<template>
  <div class="main">
    <!-- 图像文件：image/*（所有图片类型）、image/jpeg、image/png、image/gif
    视频文件：video/*（所有视频类型）、video/mp4、video/quicktime
    音频文件：audio/*（所有音频类型）、audio/mpeg、audio/wav
    文档文件：application/pdf、application/msword、application/vnd.ms-excel
    压缩文件：application/zip、application/x-gzip -->
    <input class="file" name="file" type="file" accept="file_extension" @change="updateFace" v-show="false" multiple ref="input" />
    <el-button type="primary" :loading="loading" @click="inputClick" v-if="fileList.length<limit">选取附件</el-button>
    <!-- 上传提示 -->
    <div class="el-upload__tip" v-if="showTip==1">
      请上传
      <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
      <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
      的文件
    </div>
    <!-- 文件列表 -->
    <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
      <li :key="index" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
        <el-link :href="file" :underline="false" target="_blank">
          <span class="el-icon-document"> {{ file }} </span>
        </el-link>
        <div class="ele-upload-list__item-content-action">
          <el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
        </div>
      </li>
    </transition-group>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
import useCounterStore from "@/store/counter";
const counter = useCounterStore();
const { proxy } = getCurrentInstance();
const props = defineProps([
  "fileSize",
  "showTip",
  "modelValue",
  "valType",
  "limit",
]);
const emit = defineEmits(["update:modelValue"]);

let fileType = ref(["doc", "xls", "ppt", "txt", "pdf", "docx"]);
let loading = ref(false);
let fileSize = computed(() => {
  return props.fileSize ? props.fileSize : 5;
});
let showTip = computed(() => {
  return props.showTip ? props.showTip : 1;
});
let limit = computed(() => {
  return props.limit ? props.limit : 1;
});
const name = computed({
  get() {
    return props.modelValue;
  },
  set(value: Number) {
    emit("update:modelValue", value);
  },
});
let fileList = ref([]); //文件列表

let inputClick = () => {
  proxy.$refs.input.click();
};

// 删除文件
function handleDelete(index) {
  fileList.value.splice(index, 1);
}

let updateFace = async (e) => {
  try {
    if (e.target.files.length == 0) return;
    let file = e.target.files;
    loading.value = true;
    let imgsArr = [...fileList.value];
    for (let index = 0; index < file.length; index++) {
      const element = file[index];
      let resp = await counter.uploadFile(element);
      imgsArr.push(resp);
    }
    fileList.value = [...imgsArr];
  } catch (error) {
  } finally {
    loading.value = false;
    e.target.value = "";
  }
};

// 当值发生变化，更新值
watch(
  () => props.modelValue,
  (newValue, oldValue) => {
    if (newValue) {
      let arr = props.valType == "arr" ? newValue : newValue.split(",");
      fileList.value = [...arr];
    } else {
      fileList.value = [];
    }
  },
  { immediate: true, deep: true }
);

watch(
  () => fileList.value,
  (newValue, oldValue) => {
    if (newValue) {
      let con = props.valType == "arr" ? newValue : newValue.toString();
      emit("update:modelValue", con);
    }
  },
  { deep: true, immediate: true }
);
</script>



<style lang="scss" scoped>
.main {
  width: 100%;
}
.upload-file-list .el-upload-list__item {
  border: 1px solid #e4e7ed;
  line-height: 2;
  margin-bottom: 10px;
  position: relative;
}

.upload-file-list .ele-upload-list__item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: inherit;
}

.ele-upload-list__item-content-action .el-link {
  margin-right: 10px;
}
</style>
